import React, { useEffect, useState, useMemo } from 'react';
import { Space, Select, Button } from 'antd';
import { connect } from 'umi';
import { facListAll, tableListAll } from './setServers';
import BuildSetTable from './BuildSetTable';

import styles from '@/assets/setting/BuildSet.less';

const { Option } = Select;

const buildSet = (props: any) => {
  console.log('buildSet props 数据 !!!!!!!', props);
  const { dvaState } = props;

  // const [facList, setFacList] = useState([]);
  const [tableData, setTableData] = useState([]);

  // const getFacList = () => {
  //   facListAll().then((res) => {
  //     setFacList(res['data']);
  //   });
  // };
  const getTableListAll = () => {
    tableListAll().then((res) => {
      // console.log('请求成功', res);
      res.data.length &&
        res['data'].forEach((item: any, i: number) => {
          item['key'] = item['id'] + item['siteid'] + i;
        });
      setTableData(res['data']);
    });
  };

  useEffect(() => {
    // const promiseList = [].map(()=>{})
    // [getFacList, getTableListAll].map(async (Fun: Function) => {
    //   await Fun();
    // });
    getTableListAll();
    // console.log('promiseList !!!', promiseList);

    // })
  }, []);

  const buildSetTab = useMemo(() => {
    return (
      <BuildSetTable
        getTableListAll={getTableListAll}
        facList={dvaState['facListAll']}
        tableData={tableData}
      />
    );
  }, [tableData]);

  return (
    <div className={styles['buildSetWrapper']}>
      <h2>建物资讯设定</h2>
      <Space>
        <Select defaultValue="" className={styles['selectWrap']}>
          {dvaState['facListAll'].length &&
            dvaState['facListAll'].map((fac: any) => {
              return (
                <Option key={fac['ename']} value={fac['ename']}>
                  {fac['ename']}
                </Option>
              );
            })}
        </Select>
        <Button type="primary">查询</Button>
      </Space>

      {buildSetTab}
    </div>
  );
};

/* 拿到仓库中的数据 */
const mapStateProps = (state: any) => {
  // setting 指命名空间
  return { dvaState: state.setting };
};
export default connect(mapStateProps)(buildSet);
